package com.xy.novelmanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xy.model.Novel;
import com.xy.model.NovelType;
import com.xy.model.PageList;
import com.xy.model.RootNovelType;
import com.xy.novelmanagement.mapper.NovelManagementMapper;
import com.xy.novelmanagement.service.INovelManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NovelManagementServiceImpl implements INovelManagementService {
		@Autowired
		private NovelManagementMapper novelMapper;

		@Override
		public PageList<Novel> getAll(Integer pageNum, Integer pageSize) {
				if (pageNum==null){
						pageNum = 1;
				}
				if(pageSize==null){
						pageSize=10;
				}
				PageHelper.startPage(pageNum,pageSize);
				List<Novel> novelList = novelMapper.selectAll();
				return new PageList<>(novelList);
		}

		@Override
		@Transactional
		public Integer downShelfNovel(String[] nameList) {
				int num = 0;
				try {
						num = novelMapper.downShelfNovel(nameList);
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return num;
		}

		@Override
		@Transactional
		public Integer recoverNovel(String[] nameList) {
				int recoverNum = 0;
				try {
						recoverNum = novelMapper.recoverNovel(nameList);
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return recoverNum;
		}

		@Override
		public List<RootNovelType> getTypeList() {
				List<RootNovelType> list=null;
				try {
						list = novelMapper.getTypeList();
				}catch (Exception e){
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return list;
		}

		@Override
		@Transactional
		public void updateNovelType(Novel novel) {
				novelMapper.deleteNovelType(novel.getBookName());
				List<NovelType> list =new ArrayList<>();
				for(String tp:novel.getBookType()){
						NovelType novelType = new NovelType(novel.getBookName(),tp);
						list.add(novelType);
				}
				novelMapper.addNovelType(list);
		}

		@Override
		public List<Novel> selectAll() {
				return novelMapper.selectAll();
		}
}
